package com.liruan.rest.codegenerator;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.liruan.rest.po.UserAccountPO;

public class ResultMapAnnotationGenerator
{
	public static void generate(Class<?> poClass)
	{
		if (poClass == null)
		{
			return;
		}

		Field[] fields = poClass.getDeclaredFields();
		if (fields.length == 0)
		{
			return;
		}

		Pattern pattern = Pattern.compile("([A-Z])");
		StringBuilder builder = new StringBuilder();
		builder.append("@Results(id=\"baseMap\",value={");
		for (int i = 0; i < fields.length; i++)
		{
			Field field = fields[i];
			String fieldName = field.getName();

			Matcher matcher = pattern.matcher(fieldName);
			while (matcher.find())
			{

				char first = matcher.group(1).charAt(0);
				fieldName = matcher.replaceFirst("_" + Character.toLowerCase(first));
				matcher = pattern.matcher(fieldName);
			}
			String columnName = fieldName;
			fieldName = field.getName();
			if (i != 0)
			{
				builder.append(",");
			}

			builder.append("@Result(column = \"").append(columnName).append("\", property = \"").append(fieldName).append("\")");
		}

		builder.append("})\n");
		System.out.println(builder.toString());

	}

	public static void main(String[] args)
	{
			generate(ImStaffStatusTransitionHistoryPO.class);
	}
}
